﻿//页面加载初始化miniui控件
$(document).ready(function () {
    mini.parse();
    //禁止后退键 作用于Firefox、Opera
    document.onkeypress = _PageCommon.banBackSpace;
    //禁止后退键  作用于IE、Chrome
    document.onkeydown = _PageCommon.banBackSpace;
});

var _PageCommon = {};
var _Popups = {};
var _UrlParams = {}; //url参数截取对象
var _DataHelper = {};
var _JsonHelper = {};

_Popups.open = function (url, title, wd, hg, loadCallBack, closeCallback) {//弹出窗体
    var parentBaseURL = document.URL.substring(0, document.URL.lastIndexOf('/') + 1);
    mini.open({
        url: parentBaseURL + url,
        allowDrag: false,
        title: title,
        width: wd,
        height: hg,
        onload: function () {//窗体加载完毕时
            if (loadCallBack != null) loadCallBack();
        },
        ondestroy: function (action) {//窗体关闭时
            if (closeCallback != null) closeCallback(action);
        }
    });
}

_Popups.close = function (action) { //关闭弹出窗行为（确认 | 取消）  参数action：ok | cancle
    if (window.CloseOwnerWindow)
        return window.CloseOwnerWindow(action);
    else
        window.close();
}

_UrlParams.convertKeyVal = function (url) {//截取URL中参数以键值对返回
    if (!url) url = location.href;
    url = url.split('?')[1];
    var params = {};
    if (url) {
        var us = url.split("&");
        for (var i = 0, l = us.length; i < l; i++) {
            var ps = us[i].split("=");
            params[ps[0]] = decodeURIComponent(ps[1]);
        }
    }
    return params;
}

_JsonHelper.getText = function (json, id) { //根据id获取CodeData的text
    for (var i = 0; i < json.length; i++) {
        if (json[i].id !== "" && json[i].id == id)
            return json[i].text;
    }
    return "";
}

_DataHelper.GetDateInterval = function (d) {
    var handleDate = null;
    var currentDate = new Date();
    currentDate.setHours(0, 0, 0, 0);
    switch (d) {
        case "0":
            handleDate = currentDate;
            break;
        case "7":
            handleDate = new Date(currentDate.getTime() - (1000 * 60 * 60 * 24 * 7));
            break;
        case "30":
            handleDate = new Date(currentDate.getTime() - (1000 * 60 * 60 * 24 * 30));
            break;
        case "week":
            var week = currentDate.getDay() == 0 ? 7 : currentDate.getDay();
            handleDate = new Date(currentDate.getTime() - (1000 * 60 * 60 * 24 * (week - 1)));
            break;
        case "month":
            handleDate = new Date(currentDate.getFullYear() + "/" + (currentDate.getMonth() + 1) + "/1");
            break;
    }
    return { "start": handleDate, "end": currentDate };
}

//处理键盘事件 禁止后退键（Backspace）密码或单行、多行文本框除外
_PageCommon.banBackSpace = function (e) {
    var ev = e || window.event; //获取event对象   
    var obj = ev.target || ev.srcElement; //获取事件源   

    var t = obj.type || obj.getAttribute('type'); //获取事件源类型  

    //获取作为判断条件的事件类型
    var vReadOnly = obj.getAttribute('readonly');
    var vEnabled = obj.getAttribute('enabled');
    //处理null值情况
    vReadOnly = (vReadOnly == null) ? false : vReadOnly;
    vEnabled = (vEnabled == null) ? true : vEnabled;

    //当敲Backspace键时，事件源类型为密码或单行、多行文本的，
    //并且readonly属性为true或enabled属性为false的，则退格键失效
    var flag1 = (ev.keyCode == 8 && (t == "password" || t == "text" || t == "textarea")
                && ((vReadOnly == true || vReadOnly == "true") /*|| (vEnabled != true || vEnabled == "false")*/)) ? true : false;

    //当敲Backspace键时，事件源类型非密码或单行、多行文本的，则退格键失效
    var flag2 = (ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea")
                ? true : false;

    //判断
    if (flag2) {
        return false;
    }
    if (flag1) {
        return false;
    }
}